package kbproposal

import (
	"context"
	"time"

	"mall/service/backend/common/i18n"
	"mall/service/blog/rpc/internal/svc"
	"mall/service/blog/rpc/internal/utils/errorhandler"
	"mall/service/blog/rpc/types/blog"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendUpdateProposalLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendUpdateProposalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendUpdateProposalLogic {
	return &BackendUpdateProposalLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// group: kbproposal
func (l *BackendUpdateProposalLogic) BackendUpdateProposal(in *blog.ProposalInfo) (*blog.BaseResp, error) {
	data, err := l.svcCtx.KBProposal.FindOneNoCache(l.ctx, nil, *in.Id)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	if in.Status != nil {
		data.PostStatus = uint16(*in.Status)
	}
	data.PostModifiedDate = time.Now()
	err = l.svcCtx.KBProposal.Update(l.ctx, nil, data)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	return &blog.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}
